import { ENC_TYPE } from "../api/enums.js"
import {CryptoUtil} from "@/api/cryptoUtil";
export default {
	state: {
		ecriptionType: null,
		primaryKey: null,
		secondaryKey: null,
		instance: {
			cryptoUtil: new CryptoUtil(1),
			code: 1,
			keyObj: null,
			keyForKeyObj: null,
			sendCke: null,
		}
	},

	mutations: {
		setCryptoInstance(state, instance) {
			state.instance = instance
		},
		setEcriptionType(state, type) {
			state.ecriptionType = type
		},
		setPrimaryKey(state, key) {
			state.primaryKey = key
		},
		setSecondaryKey(state, key) {
			state.secondaryKey = key
		},
		clear(state) {
			state.ecriptionType = null
			state.primaryKey = null
			state.secondaryKey = null
		}
	},
	actions: {
		// loadEncryption(context, code) {
		loadEncryption(context) {
			return new Promise((resolve, reject) => {
				var code = Number(sessionStorage.getItem("code"));
				var cryptoUtil = new CryptoUtil(code);
				var keyObj = cryptoUtil.randomKeyObj(null);
				var keyForKeyObj = cryptoUtil.randomKeyObj(sessionStorage.getItem("psha256"))
				var sendCke = cryptoUtil.encode(keyForKeyObj, JSON.stringify(keyObj));

				var instance = {
					code,
					cryptoUtil,
					keyObj,
					keyForKeyObj,
					sendCke,
				}

				context.commit("setCryptoInstance", instance);
				resolve();
			})

		}
	},
	getters: {
		cryptoInstance: (state) => () => {
			return state.instance
		},
	}
}